﻿using Microsoft.Extensions.Configuration;
using System;
using ZhongKeSite.Common.Configuration;
using ZhongKeSite.Common.Helper;

namespace ZhongKeSite.Common
{
    public class AppSettings
    {
        /// <summary>
        /// 验证码Cookie名字
        /// </summary>
        public static string LoginVerifyCookieName
        {
            get
            {
                try
                {
                    return ConfigurationService.GetConfiguration().GetSection("AppSettings").GetSection("LoginVerifyCookieName").Value;
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    return "bruce_login_verify_cookie";
                }
            }
        }
        /// <summary>
        /// 验证码cookie名字
        /// </summary>
        public static string VerifyCodeSecret
        {
            get
            {
                try
                {
                    return ConfigurationService.GetConfiguration().GetSection("AppSettings").GetSection("VerifyCodeSecret").Value;
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    return "bruce#_verify*_7cookie%3secret@8";
                }
            }
        }
        /// <summary>
        /// cookie有效时长(秒)
        /// </summary>
        public static int CookieExpire
        {
            get
            {
                try
                {
                    string cookieExpire = ConfigurationService.GetConfiguration().GetSection("AppSettings").GetSection("CookieExpire").Value;
                    return Convert.ToInt32(cookieExpire);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    return 7200;
                }
            }
        }
        /// <summary>
        /// 每日登录错误限制次数
        /// </summary>
        public static int LoginErrorLimitCount
        {
            get
            {
                try
                {
                    string cookieExpire = ConfigurationService.GetConfiguration().GetSection("AppSettings").GetSection("LoginErrorLimitCount").Value;
                    return Convert.ToInt32(cookieExpire);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    return 5;
                }
            }
        }
        /// <summary>
        /// 文件服务器域名
        /// </summary>
        public static string FileDomain
        {
            get
            {
                try
                {
                    return ConfigurationService.GetConfiguration().GetSection("AppSettings").GetSection("FileDomain").Value;
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    return "http://localhost";
                }
            }
        }
        /// <summary>
        /// 当前域名
        /// </summary>
        public static string Domain
        {
            get
            {
                try
                {
                    return ConfigurationService.GetConfiguration().GetSection("AppSettings").GetSection("Domain").Value;
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    return "http://localhost";
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string UploadDirPath
        {
            get
            {
                var def = "\\Files\\Upload";
                try
                {
                    string value = ConfigurationService.GetConfiguration().GetSection("AppSettings").GetSection("UploadDirPath").Value;
                    def = string.IsNullOrWhiteSpace(value) ? def : value;
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                }
                return def;
            }
        }

        /// <summary>
        /// 通用导出表格模版地址
        /// </summary>
        public static string OutputExcelTemplatePath
        {
            get
            {
                var def = "\\Files\\templates\\excel_output.xls";
                try
                {
                    string value = ConfigurationService.GetConfiguration().GetSection("AppSettings").GetSection("OutputExcelTemplatePath").Value;
                    def = string.IsNullOrWhiteSpace(value) ? def : value;
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                }
                return def;
            }
        }

        /// <summary>
        /// ConnString
        /// </summary>
        public static string ConnString
        {
            get
            {
                var def = "Data Source=localhost;Initial Catalog=ZhongKeDB; user id=sa;pwd=xiaopan123456;";
                try
                {
                    string value = ConfigurationService.GetConfiguration().GetConnectionString("DefaultConnection");
                    def = string.IsNullOrWhiteSpace(value) ? def : value;
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                }
                return def;
            }
        }

    }
}
